#include "tcp_server.h"
#include "session.h"

#include <iostream>

void tcp_server::do_accept()
{
    // auto self = shared_from_this();
    acceptor_.async_accept(
        [this](boost::system::error_code ec, tcp::socket socket) {
            if (ec) {
                std::cerr << "accept failed: " << ec.message() << std::endl;
                return;
            }

            std::cout << "a client connected. peer address: " << socket.remote_endpoint().address().to_string() <<
                std::endl;

            /* create session */
            auto session_ptr = std::make_shared<session>(std::move(socket));
            session_ptr->start();
            sessions_.insert(session_ptr);
            do_accept();
        });
}
